#!/bin/bash

# 用法：
# ./clean_jobs.sh input.csv output.csv
# 说明：
# - 输入文件 input.csv 应该已经去除字段标题行
# - 输出文件 output.csv 将是清洗后的结果，不包含表头

if [ $# -ne 2 ]; then
    echo "用法: $0 input.csv output.csv"
    exit 1
fi

input_file="$1"
output_file="$2"

gawk -F',' -v OFS=',' '
BEGIN { id = 1 }

{
    
    if (NF != 7 || $6 !~ /人$/) next

    
    type   = $1
    title  = $2
    salary = $3
    
    comp_type = $5
    scale     = $6
    location  = $7

    
    min = 0; max = 0; multi = 12

    if (salary ~ /\/年/) {
        gsub(/\/年/, "", salary)
        if (match(salary, /^([0-9.]+)-([0-9.]+)万$/, m)) {
            min = m[1] * 10000
            max = m[2] * 10000
        } else {
            gsub(/[^0-9.]/, "", salary)
            min = max = salary * 10000
        }
    } else {
        if (match(salary, /·([0-9]+)薪/, m)) {
            multi = m[1]
        }
        gsub(/·[0-9]+薪/, "", salary)

        if (match(salary, /^([0-9.]+)-([0-9.]+)([千万])$/, m)) {
            unit = m[3] == "万" ? 10000 : 1000
            min = m[1] * unit * multi
            max = m[2] * unit * multi
        } else if (match(salary, /^([0-9.]+)([千万])-([0-9.]+)([千万])$/, m)) {
            unit1 = m[2] == "万" ? 10000 : 1000
            unit2 = m[4] == "万" ? 10000 : 1000
            min = m[1] * unit1 * multi
            max = m[3] * unit2 * multi
        } else {
            gsub(/[^0-9.]/, "", salary)
            min = max = salary * multi
        }
    }

    salary_mid = int((min + max) / 2)

    scale_mid = -1
    if (match(scale, /([0-9]+)-([0-9]+)人/, m)) {
        scale_mid = int((m[1] + m[2]) / 2)
    } else if (match(scale, /([0-9]+)人/, m)) {
        scale_mid = m[1]
    } else {
        next 
    }

    
    if (index(location, "·") > 0) {
        split(location, loc_parts, "·")
        city = loc_parts[1]
    } else {
        city = location
    }

    
    print id, type, title, salary_mid, comp_type, scale_mid, city
    id++
}
' "$input_file" > "$output_file"

